package com.uetoken.cn.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.EncodeUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.uetoken.cn.R;
import com.uetoken.cn.base.BaseActivity;
import com.uetoken.cn.common.ApiHelper;
import com.uetoken.cn.common.Constant;
import com.uetoken.cn.network.OkHttpUtils;
import com.uetoken.cn.network.Params;
import com.uetoken.cn.network.WebResponse;
import com.uetoken.cn.utils.CountDownTimerUtils;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * @author xieyang
 * @date 2018年8月22日15:17:40
 * 忘记密码
 */
public class ForgetPasswordActivity extends BaseActivity implements WebResponse {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.edt_activity_forget_password_phone)
    EditText mEdtPhone;
    @BindView(R.id.tv_activity_forget_password_get_auth_code)
    TextView mTvGetAuthCode;
    @BindView(R.id.edt_activity_forget_password_input_auth_code)
    EditText mEdtAuthCode;
    @BindView(R.id.edt_activity_forget_password_input_six_to_sixteen_pw)
    EditText mEdtAInputPwd;
    @BindView(R.id.edt_activity_forget_password_input_again)
    EditText mEdtInputAgain;
    @BindView(R.id.tv_activity_forget_password_confirm_modify)
    TextView mTvConfirmModify;
    @BindView(R.id.tv_activity_forget_password_setting_pwd)
    TextView mTvSettingPwd;
    /**
     * 获取验证码
     */
    private static final int REQUEST_NETWORK_GET_AUTH_CODE = 10;
    /**
     * 确认修改(登录或支付密码)请求码
     */
    private static final int REQUEST_NETWORK_CONFIRM_MODIFY = 20;

    private CountDownTimerUtils countDownTimerUtils;
    /**
     * 用于区分是修改支付密码还是修改登录密码
     */
    private String modifypwd;
    /**
     * 用于区分是忘记登录密码还是修改登录密码
     */
    private String loginPwd;

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        //状态栏沉浸式
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this, android.R.color.white), true);
        BarUtils.setStatusBarLightMode(this, true);
        mIvBack.setVisibility(View.VISIBLE);
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        modifypwd = intent.getStringExtra("pwd");
        if (modifypwd.equals("login")) {
            loginPwd = intent.getStringExtra("modify");
            mEdtAInputPwd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});
            mEdtInputAgain.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});
            if (loginPwd.equals("forget")) {
                //从登录界面跳转过来的
                mTvTitle.setText(getResources().getString(R.string.tv_forget_password));
                mTvSettingPwd.setText(getResources().getString(R.string.str_setting_new_login_pwd));
                mEdtAInputPwd.setHint(getResources().getString(R.string.str_input_new_pwd));
                mEdtInputAgain.setHint(getResources().getString(R.string.str_input_new_pwd_again));
            } else if (loginPwd.equals("modifylogin")) {
                //从安全中心的修改登录密码过来的
                mEdtPhone.setText(SPUtils.getInstance(Constant.UE_TOKEN_PREFERENCE_USER_INFO).getString(Constant.SP_MOBILE_NO, ""));
                mEdtPhone.setFocusable(false);
                mTvTitle.setText(getResources().getString(R.string.str_modify_password));
                mTvSettingPwd.setText(getResources().getString(R.string.str_setting_password));
                mEdtAInputPwd.setHint(getResources().getString(R.string.str_please_input_password));
                mEdtInputAgain.setHint(getResources().getString(R.string.str_please_input_password_again));
            }

        } else if (modifypwd.equals("pay")) {
            mEdtAInputPwd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
            mEdtInputAgain.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
            //从安全中心修改支付密码或者未设置支付密码弹窗跳转过来的
            mTvTitle.setText(getResources().getString(R.string.str_modify_pay_pwd));
            mTvSettingPwd.setText(getResources().getString(R.string.str_setting_pay_pwd));
            //修改支付密码
            mEdtPhone.setText(SPUtils.getInstance(Constant.UE_TOKEN_PREFERENCE_USER_INFO).getString(Constant.SP_MOBILE_NO, ""));
            mEdtPhone.setFocusable(false);
            mEdtAInputPwd.setHint(getResources().getString(R.string.str_please_intput_payment_password));
            mEdtInputAgain.setHint(getResources().getString(R.string.str_please_inputt_pwd_again));
        }

    }

    @Override
    public void init() {

    }

    @OnClick({R.id.iv_back, R.id.tv_activity_forget_password_get_auth_code, R.id.tv_activity_forget_password_confirm_modify})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_activity_forget_password_get_auth_code:
                getAuthCode();
                break;
            case R.id.tv_activity_forget_password_confirm_modify:
                confirmModify();
                break;
        }
    }

    /**
     * 获取验证码
     */
    private void getAuthCode() {
        String phone = mEdtPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort(getResources().getString(R.string.str_phone_not_be_empty));
            return;
        }
        if (!RegexUtils.isMobileSimple(phone)) {
            ToastUtils.showShort(getResources().getString(R.string.str_phone_not_specification));
            return;
        }
        countDownTimerUtils = new CountDownTimerUtils(this, mTvGetAuthCode, 60000, 1000);
        countDownTimerUtils.start();
        OkHttpUtils.getInstance().getStringWithParam(this, ApiHelper.getSendSMSUrl(), REQUEST_NETWORK_GET_AUTH_CODE, Params.getSmsParams(phone));
    }

    /**
     * 确认修改
     */
    private void confirmModify() {
        String phone = mEdtPhone.getText().toString().trim();
        String authCode = mEdtAuthCode.getText().toString().trim();
        String pwd = mEdtAInputPwd.getText().toString().trim();
        String confirmPwd = mEdtInputAgain.getText().toString().trim();

        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort(getResources().getString(R.string.str_phone_not_be_empty));
            return;
        }
        if (!RegexUtils.isMobileSimple(phone)) {
            ToastUtils.showShort(getResources().getString(R.string.str_phone_not_specification));
            return;
        }
        if (TextUtils.isEmpty(authCode)) {
            ToastUtils.showShort(getResources().getString(R.string.str_auth_code_not_empty));
            return;
        }
        if (pwd.length() < 6 || pwd.length() > 16) {
            ToastUtils.showShort(getResources().getString(R.string.str_pwd_not_specification));
            return;
        }
        if (!pwd.equals(confirmPwd)) {
            ToastUtils.showShort(getResources().getString(R.string.str_two_pwd_different));
            return;
        }
        if (modifypwd.equals("login") && loginPwd.equals("forget")) {
            //调用忘记密码接口
            showLoading();
            OkHttpUtils.getInstance().getStringWithParam(this, ApiHelper.getForgetPwdUrl(), REQUEST_NETWORK_CONFIRM_MODIFY, Params.getForgetPwdParams(
                    phone, authCode, EncodeUtils.base64Encode2String(pwd.getBytes())));
        } else {
            String type = "";
            if (modifypwd.equals("login")) {
                //修改登录密码
                type = "5";
                //修改支付密码
            } else if (modifypwd.equals("pay")) {
                type = "4";
            }
            //调用修改支付(登录)密码接口
            showLoading();
            OkHttpUtils.getInstance().getStringWithParam(ForgetPasswordActivity.this, ApiHelper.getModifyUserInfoUrl(), REQUEST_NETWORK_CONFIRM_MODIFY,
                    Params.getModifyUserInfoParams(type, EncodeUtils.base64Encode2String(confirmPwd.getBytes()), authCode));
        }
    }

    @Override
    public void onSuccessResponse(Call call, String response, int requestCode) throws IOException {
        switch (requestCode) {
            case REQUEST_NETWORK_GET_AUTH_CODE:
                break;
            case REQUEST_NETWORK_CONFIRM_MODIFY:
                dissmissDialog();
                JsonObject obj = new JsonParser().parse(response).getAsJsonObject();
                String message = obj.get("message").getAsString();
                if (obj.get("result").getAsInt() > 0) {
                    SPUtils.getInstance(Constant.UE_TOKEN_PREFERENCE_USER_INFO).put(Constant.IS_PAY_PWD, false);
                    ToastUtils.showShort(message);
                    finish();
                } else {
                    ToastUtils.showShort(message);
                }

                break;
        }
    }

    @Override
    public void onFailResponse(Call call, IOException e, int requestCode) {
        dissmissDialog();
        ToastUtils.showShort("错误码："+requestCode );
    }
}
